package com.redpack.redpost.struts.action.admin;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.redpack.redpost.struts.form.admin.slideForm;
import com.redpack.redpost.util.dao.adminDAO;
import com.redpack.redpost.util.to.generalUtil;
import com.redpack.redpost.util.to.slidesTo;

public class editarslide extends Action {
	
	public ActionForward execute(
			ActionMapping mapping, 
			ActionForm form, 
			HttpServletRequest request, 
			HttpServletResponse response) throws Exception {
		
		boolean logginVal = generalUtil.isLoggedInAdmin(request);
		if(logginVal!=true){
			return mapping.findForward("noLogin") ;
		}
		
		slideForm f = (slideForm)form;
		
		if( f != null ){
			if( f.getAcc().equals("2") ){
				adminDAO.eliSlide(f.getCve_slide());
			}
			
			if( f.getAcc().equals("1") ){
				List contenidos = new ArrayList();
				try {
					contenidos = adminDAO.getContenidos();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				request.setAttribute("contenidos", contenidos);
		
				slidesTo slide_info = adminDAO.getSlide( f.getCve_slide() );
				
				String[][] idiomas = generalUtil.obtieneIdiomas();
				request.setAttribute("idiomas", idiomas);
				
				request.setAttribute("nombre_sel", slide_info.getNombre());
				request.setAttribute("cve_slide", ""+slide_info.getCve_slide());
				request.setAttribute("contenido_sel", ""+slide_info.getContenido());
				request.setAttribute("idioma_sel", ""+slide_info.getIdioma()+"");
				
				return mapping.findForward("edit") ;
			}
		}
		
		List slides = adminDAO.getSlides();
		request.setAttribute("slides", slides);
		
		return mapping.findForward("success") ;
	}

}